<%@ page language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<script type="text/javascript">
//创建菜单
function createMenu(list,appendNode){
	//添加非叶子菜单
	//如果不是一级菜单,需添加<ul class='submenu'></ul>
	for(var i =0;i<list.length;i++){
		var menuObject = list[i];
		var menuId = menuObject.id;
		var menuName = menuObject.name;
		var url = menuObject.url=="/"?"#":menuObject.url;
		
		 /* <ul id="navMenuRoot" class="nav navbar-nav">
               <li class="active"><a href="#">Home</a></li>
               <li class="dropdown">
                   <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
                   <ul class="dropdown-menu">
                       <li><a href="#">Action</a></li>
                       <li><a href="#">Another action</a></li>
                       <li><a href="#">Something else here</a></li>
                       <li class="divider"></li>
                       <li class="dropdown-header">Nav header</li>
                       <li><a href="#">Separated link</a></li>
                       <li><a href="#">One more separated link</a></li>
                   </ul>
               </li> 
           </ul> */
           
            //判断是否为子节点 添加有节点的菜单
		if(menuObject.hasChild == true){
			
			appendNode.append("<li class='dropdown' id ="+menuId+">" +
										//点击展现下级
										"<a href='#' class='dropdown-toggle' data-toggle='dropdown'>"+
									 	/*  //图标
										"<i class='icon-dashboard'></i>" +   */
										//显示text
										"<span class='menu-text'>"+menuName +"</span>" +
										//下级按钮
										"<b class='caret'></b>"+
									    "</a>" +
									    //下级节点添加到submenu下
									    "<ul class='dropdown-menu' id='"+menuId+"_ul' class='submenu' text='"+menuName+"'></ul>"+
							 	"</li>");
			var children = menuObject.data;
			createMenu(children,$("#"+menuId+"_ul"));
		}else{
		
			//添加菜单
			appendNode.append("<li id ='"+menuId+"'>"+
										"<a href='#' onclick=menuClick('"+url+"','"+menuId+"','"+menuName+"')>"+
										menuName+
									"</a>"+
								"</li>");
		}
	}
}
//菜单项点击事件-页面跳转,链接路径修改
function menuClick(url,menuId,menuName){
	//新添加了url==0 long
	if(url==null|| url==0|| url=="null" || url==""){
		return false;
	}
	window.location.href = "${ctx}" + url+"?menuId="+menuId+"&menuName="+encodeURIComponent(menuName);
}


$(function(){	
	//查询菜单
	Otod.ajax({  
		url:"${ctx}/sys/menu/getMenu.do",
		type:"GET",
		successFn: function(data){
	        	//动态设置菜单
	        	createMenu(data,$("#navMenuRoot"));
	    }
	});
});
</script>
